package org.internna.expensetracker.model;

import java.util.Set;
import java.util.HashSet;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import javax.validation.constraints.NotNull;
import org.internna.expensetracker.model.Account;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooEntity
public class AccountType {

    @NotNull
    private Boolean business;

    @NotNull
    private String typeKey;

    @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "accountType")
    private Set<Account> accounts = new HashSet<Account>();

    public static final AccountType findAccountTypeByKey(String typeKey) {
    	return entityManager().createQuery("select a from AccountType a where typeKey = :typeKey", AccountType.class)
    		.setParameter("typeKey", typeKey)
    		.getSingleResult();
    }

}
